/**
 * 
 */
package org.istt.export;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.istt.domain.File;

/**
 * @author Ndtho
 *
 */
public class ReceivedFiles extends BaseReport {
	static int FONT_SIZE_8 = 8;
	static int FONT_SIZE_10 = 10;
	public InputStream toMSWord(InputStream docFile, List<File> files, String department, List<Object[]> trs, Date fromD, Date toD) throws IOException {
	    
//		List<FileBean> lstFileBean = new ArrayList<FileBean>();
		
		
	    XWPFDocument docx = new XWPFDocument(docFile);
	    Iterator<XWPFTable> range=docx.getTablesIterator();

	    //Header
        XWPFTable tableHeader = range.next();
        
        List<XWPFParagraph> paragraphs = docx.getParagraphs();
        
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm");
		String fromDate = dateFormat.format(fromD);
		String toDate = dateFormat.format(toD);
		String strToday = "T\u1EEB " + fromDate.substring(12, 16) + " ng\u00E0y "+fromDate.substring(0, 10) + 
							" \u0111\u1EBFn " + toDate.substring(12, 16) + " ng\u00E0y "+toDate.substring(0, 10);
        
		
		/*==================== PARAGHRAPH ====================*/
		for (int x=0; x<paragraphs.size();x++)
		{
			XWPFParagraph paragraph = paragraphs.get(x);
			XWPFRun r = paragraph.createRun();
			switch(x)
			{
			  case 1: //Ngay
				  setTextFormat(r, strToday, FONT_SIZE_10, true, true, FONT_TIMES_NEW_ROMAN);
				  break;
			  case 2: //Phong ban
				  setTextFormat(r, department, FONT_SIZE_10, true, false, FONT_TIMES_NEW_ROMAN);
				  break;
			}
		}
		
        /*==================== TABLE ====================*/
		int counter = 1;
		for (Iterator iterator = files.iterator(); iterator.hasNext();) {
			File filecontent = (File) iterator.next();
			
			//FileBean bean = getResultOfFile(file, fromDate, toDate, weekends, holidays)
			
			XWPFTableRow content_row = tableHeader.createRow();
			
			//STT
	        XWPFRun colum1 = content_row.getCell(0).getParagraphs().get(0).createRun();
	        setAlignment(content_row.getCell(0).getParagraphs().get(0), CENTER_ALIGN, SPACING_0, SPACING_06);
	        setTextFormat(colum1, filecontent.getNumber()+"", FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);

	        //SENDER
	        XWPFRun colum2 = content_row.getCell(1).getParagraphs().get(0).createRun();
	        setAlignment(content_row.getCell(1).getParagraphs().get(0), CENTER_ALIGN, SPACING_0, SPACING_06);
	        setTextFormat(colum2, filecontent.getSubmitter(), FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);
	        XWPFRun colum21 = content_row.getCell(1).getParagraphs().get(0).createRun();
	        colum21.addBreak();
	        setTextFormat(colum21, " (Li\u00EAn h\u1EC7: "+filecontent.getContactName() + " - " + filecontent.getContactPhone()+")", FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);

	        //LOCATION
	        XWPFRun colum3 = content_row.getCell(2).getParagraphs().get(0).createRun();
	        setAlignment(content_row.getCell(2).getParagraphs().get(0), LEFT_ALIGN, SPACING_0, SPACING_06);
	        String value = "";
	        if(StringUtils.isNotEmpty(filecontent.getLocation()))
	        	value = filecontent.getLocation();
	        else if(StringUtils.isNotEmpty(filecontent.getAddress()))
	        	value = filecontent.getAddress();
	        setTextFormat(colum3, value, FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);
	        

	        // Thu tuc hanh chinh
	        XWPFRun colum4 = content_row.getCell(3).getParagraphs().get(0).createRun();
	        String strCategoryName = filecontent.getCategory().getCategoryName();
	        if(filecontent.getSubCategory()!=null) 
	        	strCategoryName += " (" + filecontent.getSubCategory().getName() + ")";
	        setTextFormat(colum4, strCategoryName, FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);
	        setAlignment(content_row.getCell(3).getParagraphs().get(0), LEFT_ALIGN, SPACING_0, SPACING_06);

	        //INPUTDATE
	        XWPFRun colum5 = content_row.getCell(4).getParagraphs().get(0).createRun();
	        String strdate =  dateFormat.format(filecontent.getInputDate().toDate());
	        setTextFormat(colum5, strdate, FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);
	        setAlignment(content_row.getCell(4).getParagraphs().get(0), LEFT_ALIGN, SPACING_0, SPACING_06);

	        //OUTPUTDATE
	        XWPFRun colum9 = content_row.getCell(9).getParagraphs().get(0).createRun();
	        strdate =  dateFormat.format(filecontent.getOutputDate().toDate());
	        setTextFormat(colum9, strdate, FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);
	        setAlignment(content_row.getCell(9).getParagraphs().get(0), LEFT_ALIGN, SPACING_0, SPACING_06);

	        
	        XWPFRun colum10 = content_row.getCell(10).getParagraphs().get(0).createRun();
	        setTextFormat(colum10, "", FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);
	        setAlignment(content_row.getCell(10).getParagraphs().get(0), LEFT_ALIGN, SPACING_0, SPACING_06);
	        
	        XWPFRun colum11 = content_row.createCell().getParagraphs().get(0).createRun();
	        setTextFormat(colum11, "", FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);

	        XWPFRun colum12 = content_row.createCell().getParagraphs().get(0).createRun();
	        setTextFormat(colum12, "", FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);

	        XWPFRun colum13 = content_row.createCell().getParagraphs().get(0).createRun();
	        setTextFormat(colum13, "", FONT_SIZE_8, false, false, FONT_TIMES_NEW_ROMAN);
	        
	        counter++;
		}
        
	    //write to word file
	    ByteArrayOutputStream baos = new ByteArrayOutputStream();
	    docx.write(baos);
	    
	    return new ByteArrayInputStream(baos.toByteArray());
	}
}
